package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.hckj.jianzhijia.interfaces.UserInfoChangeInterface;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ChangeDescActivity extends BaseActivity implements UserInfoChangeInterface{

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.edt_company_desc)
    EditText edtCompanyDesc;
    @InjectView(R.id.btn_commit)
    Button btnCommit;

    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_desc);
        ButterKnife.inject(this);
        ((JianzhijiaApplacation)getApplication()).addUserInfoChange(this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在上传");
        progressDialog.setCancelable(false);
        initData();
        initEvent();

    }

    private void initEvent() {
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newDesc = edtCompanyDesc.getText().toString().trim();
                if (newDesc == null || newDesc.equals("")) {
                    ToastUtils.showToast(getApplicationContext(), "请输入您的企业介绍");
                    return;
                } else {
                    progressDialog.show();
                    jianzhijiaApplacation.getCompanyUser().setDescription(newDesc);
                    jianzhijiaApplacation.getCompanyUser().saveUserInfo().saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                progressDialog.dismiss();
                                ToastUtils.showToast(getApplicationContext(),"修改成功");
                                jianzhijiaApplacation.updateCompanyInfo();
                                finish();
                            } else {
                                ToastUtils.showToast(getApplicationContext(), ExceptionUtils.getExceptionInfo(e));
                            }
                        }
                    });
                }
            }
        });
    }

    private void initData() {
        edtCompanyDesc.setText(jianzhijiaApplacation.getCompanyUser().getDescription());
    }

    @Override
    public void updateCompanyInfo() {

    }

    @Override
    public void updateCompanyLogo() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((JianzhijiaApplacation) getApplication()).removeUserInfoChangeInterface(this);
    }
}
